﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Album
{
    public partial class PhotoFriend : System.Web.UI.Page
    {
        private int userID ;//暂用用户ID，写死，到时候用COOKIE替换过来
        int userAccess = 1;//访问者ID

        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            int albumID = Convert.ToInt32(Request.QueryString["AlbumId"]);
            StringBuilder strHtml = new StringBuilder(Cache["user_photoFriend"].ToString());
            strHtml = strHtml.Replace("$Header$", Cache["user_header2"].ToString());
            strHtml = strHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            strHtml = strHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            strHtml = strHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());

            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            strHtml = strHtml.Replace("$NewMsg$", newLetterCount.ToString());

            //相片列表
            StringBuilder strPhotoTabletr = new StringBuilder();
            DataTable dtPhotoList = new BLL.User.Album().PhotoList(albumID, "ID DESC", 1, 30).Tables[0];
            string tr = "<tr>{0}</tr>";
            StringBuilder sbTr = new StringBuilder();
            StringBuilder sbTd = new StringBuilder();
            string strTd = Cache["user_photoFriend_table"].ToString();
            for (int j = 0; j < dtPhotoList.Rows.Count; j++)//每1列
            {
                sbTd.Append(strTd);
                bool takePic = new AppCode.CreateDir().EqualWH(dtPhotoList.Rows[j]["FilePath"].ToString(), 100, 100);
                if (takePic)
                { sbTd = sbTd.Replace("$FilePath$", new AppCode.CreateDir().ReDraw(100, 0, (dtPhotoList.Rows[j]["FilePath"].ToString()))); }
                else
                {
                    sbTd = sbTd.Replace("$FilePath$", dtPhotoList.Rows[j]["FilePath"].ToString());
                }
                    sbTd = sbTd.Replace("$Description$", dtPhotoList.Rows[j]["Description"].ToString())
                    .Replace("$AlbumID$", albumID.ToString())
                    .Replace("$PhotoID$", dtPhotoList.Rows[j]["ID"].ToString());
                if (j == 3 | (j > 5 & (j + 1) % 4 == 0))
                {
                    sbTr.AppendFormat(tr, sbTd.ToString());
                    sbTd.Remove(0, sbTd.Length);
                }
            }

            if (sbTd.Length > 0)
            {
                sbTr.AppendFormat(tr, sbTd.ToString());
            }

            strPhotoTabletr.Append(sbTr.ToString());

            List<Jy123.Model.User_Album> listAlbum = new BLL.User_Album().GetModelList("ID='" + albumID + "'");
            //new BLL.User.Album().AlbumList_My(1,int.MaxValue,"ID='" + albumID + "'",string.Empty,out count).Tables[0]
            strHtml = strHtml.Replace("$PhotoFriend_Table$", strPhotoTabletr.ToString());
            strHtml = strHtml.Replace("$AlbumTitle$", listAlbum[0].Title);//dtPhotoList.Rows[0]["Title"].ToString());
            strHtml = strHtml.Replace("$Description$", listAlbum[0].Description);//dtPhotoList.Rows[0]["AlbumDescription"].ToString());
            Model.User_Main modelUser = new BLL.User_Main().GetModel(listAlbum[0].FUser);
            //mUser = bllUser.GetModel(userID);
            strHtml = strHtml.Replace("$UserName$", modelUser.UserName);//dtPhotoList.Rows[0]["UserName"].ToString());
            strHtml = strHtml.Replace("$UserID$", listAlbum[0].FUser.ToString());
            strHtml = strHtml.Replace("$AlbumID$", albumID.ToString());
            //strHtml = strHtml.Replace("$AlbumTitle$",listAlbum[0].Title);//dtPhotoList.Rows[0]["Title"].ToString());
            //strHtml = strHtml.Replace("$Description$", //dtPhotoList.Rows[0]["Description"].ToString());

            Response.Write(strHtml.ToString());
        }
    }
}
